package com.web.base.open.provider;

import com.blue.jdbc.Page;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.web.base.open.cache.AccountCache;
import com.web.base.open.cache.RoleAccountCache;
import com.web.base.open.cache.RoleUrlCache;
import com.web.base.open.exception.OpenException;
import com.web.base.open.facade.AccountFacade;
import com.web.base.open.message.AccountRequest;
import com.web.base.open.message.AccountResponse;
import com.web.base.open.message.RoleAccountRequest;
import com.web.base.open.message.RoleResponse;
import com.web.base.open.model.Account;
import com.web.base.open.model.Role;
import com.web.base.open.model.RoleAccount;
import com.web.base.open.service.AccountService;
import com.web.base.open.service.RoleAccountService;
import com.web.base.open.util.AccountConvertor;
import com.web.base.open.util.RoleAccountConvertor;
import com.web.base.open.util.RoleConvertor;
import com.web.core.async.redis.CacheRefreshSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component("openAccountProvider")
public class AccountProvider implements AccountFacade
{
	@Autowired
	private AccountService accountService;
	@Autowired
	private RoleAccountService roleAccountService;
	@Resource
	private CacheRefreshSender cacheRefreshSender;
	@Autowired
	private AccountCache accountCache;
	@Autowired
	private RoleAccountCache roleAccountCache;
	@Resource
	private RoleUrlCache roleUrlCache;
	
	public AccountProvider()
	{
	}

	@Override
	public AccountResponse add(AccountRequest request)
	{
		ValidationUtil.valid(request, SaveModel.class);
		Account account = AccountConvertor.toAccount(request);
		account = accountService.save(account);
		cacheRefreshSender.send(CacheRefreshSender.OPEN_ACCOUNT_CACHE, account.getId());
		AccountResponse response = AccountConvertor.toResponse(account);
		return response;
	}

	@Override
	public AccountResponse update(AccountRequest request)
	{
		ValidationUtil.valid(request, UpdateModel.class);
		Account account = AccountConvertor.toAccount(request);
		account = accountService.save(account);
		cacheRefreshSender.send(CacheRefreshSender.OPEN_ACCOUNT_CACHE, account.getId());
		AccountResponse response = AccountConvertor.toResponse(account);
		return response;
	}

	@Override
	public AccountResponse get(Integer id)
	{
		if (id == null)
			throw new OpenException("帐号ID不能为空");
			
		Account account = accountService.get(id);
		AccountResponse response = AccountConvertor.toResponse(account);
		return response;
	}

	@Override
	public Page<AccountResponse> listPage(AccountRequest request, int start, int size)
	{
		Account account = AccountConvertor.toAccount(request);
		Page<Account> page = new Page<>(size);
		page.setCurPage(start);
		page = accountService.list(account, page);
		Page<AccountResponse> responsePage = AccountConvertor.toResponse(page);
		return responsePage;
	}

	@Override
	public List<RoleResponse> listSelect(Integer id)
	{
		if (id == null)
			throw new OpenException("帐号ID不能为空");
		
		List<Role> list = roleAccountService.listSelect(id);
		List<RoleResponse> responseList = RoleConvertor.toResponse(list);
		return responseList;
	}

	@Override
	public void saveRoleAccount(RoleAccountRequest request)
	{
		ValidationUtil.valid(request, UpdateModel.class);
		RoleAccount roleAccount = RoleAccountConvertor.toRoleAccount(request);
		roleAccountService.save(roleAccount);
		cacheRefreshSender.send(CacheRefreshSender.OPEN_ROLE_ACCOUNT_CACHE, roleAccount.getAccountId());
	}

	@Override
	public String login(String name, String secret)
	{
		return accountCache.login(name, secret);
	}

	@Override
	public boolean verify(String token)
	{
		return accountCache.verify(token);
	}
	
	@Override
	public AccountResponse getByCode(String token)
	{
		Account account = accountCache.getByToken(token);
		AccountResponse response = AccountConvertor.toResponse(account);
		return response;
	}

	@Override
	public boolean auth(String token, String url)
	{
		String name = accountCache.getNameByToken(token);
		if (name == null || name.isEmpty())
			return false;
		
		Account account = accountCache.getByName(name);
		if (account == null)
			return false;
		
		List<Integer> roleList = roleAccountCache.getRoleId(account.getId());
		if (roleList == null || roleList.isEmpty())
			return false;
		
		return roleUrlCache.auth(roleList, url);
	}
	
}
